<?php

//system extend functions
function theme_before_render($controller) {
	$config_theme = C('config_theme');
	
	C('pav_product_span', pav_get_span());
	
	//panel tool
	if ($config_theme['paneltool_enabled']) {
		if(G('pavreset') ){
			setcookie ('pav_userparam_skin', null, 0, '/');
			setcookie ('pav_userparam_pattern', null, 0, '/');
			$_COOKIE['pav_userparam_skin'] = null;
			$_COOKIE['pav_userparam_pattern'] = null;
			pav_redirect(U('/'));
		}
		elseif (P('pav_userparam')) {
			$exp = time() + 60*60*24*355;
			$data = P('pav_userparam');
			setcookie ('pav_userparam_skin', $data['skin'], $exp, '/');
			setcookie ('pav_userparam_pattern', $data['body_pattern'], $exp, '/');
			pav_redirect(U('/'));
		}
		
		if (!empty($_COOKIE['pav_userparam_skin'])) {
			$config_theme['skin'] = $_COOKIE['pav_userparam_skin'];
		}
		if (!empty($_COOKIE['pav_userparam_pattern'])) {
			$config_theme['body_pattern'] = $_COOKIE['pav_userparam_pattern'];
		}
		C('config_theme', $config_theme);
	}
}

//private functions
function pav_get_span() {
	$config_theme = C('config_theme');
	switch(ROUTE) {
		case 'product/category':
			$span = $config_theme['category_product_row'];
			break;
		case 'product/product':
			$span = $config_theme['product_product_row'];
			break;
		case 'product/special':
			$span = $config_theme['special_product_row'];
			break;
		case 'product/search':
			$span = $config_theme['search_product_row'];
			break;
		case 'product/manufacturer':
			$span = $config_theme['manufacturer_product_row'];
			break;
		default:
			$span = $config_theme['home_product_row'];
	}
	return floor(12/$span);
}

function pav_redirect($url) {
	header('Status: 302');
	header('Location: ' . str_replace(array('&amp;', "\n", "\r"), array('&', '', ''), $url));
	exit();
}

function pav_mega_menu(&$categories) {
	$output = '';
	foreach ($categories['p'] as $category_id) {
		if (!$categories[$category_id]['top']) continue;
		if (isset($categories[$category_id]['sub']) && $categories[$category_id]['showsub']) {
			$output .= "<li class=\"parent dropdown\">
				<a href=\"{$categories[$category_id]['link']}\" data-toggle=\"dropdown\" class=\"dropdown-toggle\">
				<span class=\"menu-title\">{$categories[$category_id]['name']}</span><b class=\"caret\"></b>
				</a>";
			if ($categories[$category_id]['column'] > 1) {
				$output .= pav_mega_submenu_columns($categories, $category_id);
			}
			else {
				$output .= pav_mega_submenu($categories, $categories[$category_id]['sub']);
			}
			$output .= '</li>';
		}
		else {
			$output .= "<li><a href=\"{$categories[$category_id]['link']}\"><span class=\"menu-title\">{$categories[$category_id]['name']}</span></a></li>";
		}
	}

	return $output;
}


function pav_mega_submenu(&$categories, $subs) {
	$subs = explode(',', $subs);
	$output = "<div class=\"dropdown-menu level1\"><div class=\"dropdown-menu-inner\"><ul>";

	foreach ($subs as $sub) {
	if (isset($categories[$sub]['sub']) && $categories[$sub]['showsub']) {
		$output .= "<li class=\"parent dropdown-submenu\">
			<a href=\"{$categories[$sub]['link']}\" data-toggle=\"dropdown\" class=\"dropdown-toggle\">
			<span class=\"menu-title\">{$categories[$sub]['name']}</span><b class=\"caret\"></b>
			</a>";
		$output .= pav_mega_submenu($categories, $categories[$sub]['sub']);
		$output .= "</li>";
	}
	elseif ($categories[$sub]['type'] == 2) {
		$output .= "<li><div class=\"menu-content\"><div class=\"margin\">" . v5_html_entity_decode($categories[$sub]['content']) . "</div></div></li>";
	}
	else {
		$output .= "<li><a href=\"{$categories[$sub]['link']}\"><span class=\"menu-title\">{$categories[$sub]['name']}</span></a></li>";
	}
	}
	
	$output .= "</ul></div></div>";
	
	return $output;
}

function pav_mega_submenu_columns(&$categories, $parent_id) {
	$subs = explode(',', $categories[$parent_id]['sub']);
	$column = $categories[$parent_id]['column'];
	$columns = $categories[$parent_id]['columns'];
	if (strpos($columns, ',') !== FALSE) {
		$columns = explode(',', $columns);
	}
	else {
		$span = floor(12 / $column);
		$columns = array_pad(array(), $column, $span);
	}

	$groups = array();
	foreach($subs as $sub) {
		$group = $categories[$sub]['group'];
		$groups[$group][] = $sub;
	}
	
	$output = "<div class=\"dropdown-menu menu-content mega-cols cols{$column}\">
				<div class=\"dropdown-menu-inner\">
				  <div class=\"row-fluid\">";

	for($i = 1; $i <= $column; $i++) {
		$output .= "<div class=\"mega-col span{$columns[$i-1]} col-{$i}\"><ul>
		<li class=\"parent dropdown-submenu mega-group\">
		<div class=\"dropdown-mega level1\"><ul>";
		if (isset($groups[$i])) {
			foreach($groups[$i] as $sub) {
				if (isset($categories[$sub]['sub']) && $categories[$sub]['showsub']) {
					$output .= "<li class=\"parent dropdown-submenu\">
					<a href=\"{$categories[$sub]['link']}\" data-toggle=\"dropdown\" class=\"dropdown-toggle\">
					<span class=\"menu-title\">{$categories[$sub]['name']}</span><b class=\"caret\"></b>
					</a>";
					$output .= pav_mega_submenu($categories, $categories[$sub]['sub']);
					$output .= "</li>";
				}
				elseif ($categories[$sub]['type'] == 2) {
					$output .= "<li><div class=\"menu-content\"><div class=\"margin\">" . v5_html_entity_decode($categories[$sub]['content']) . "</div></div></li>";
				}
				else {
					$output .= "<li><a href=\"{$categories[$sub]['link']}\"><span class=\"menu-title\">{$categories[$sub]['name']}</span></a></li>";
				}
			}
		}
		$output .= "</ul></div></li></ul></div>";
	}

	$output .= "</div></div></div>";

	return $output;
}

//default config
return array(
	'skin' => 'default',
	'display_mode' => 'grid',
	'home_product_row' => 3,
	'category_product_row' => 3,
	'product_product_row' => 3,
	'manufacturer_product_row' => 4,
	'special_product_row' => 4,
	'search_product_row' => 3,
	'paneltool_enabled'  => 0,
	'body_pattern'  => '',
	'theme_width'   => 'auto',
	'contact' => '<ul>
		<li class="addr">XXXX, XXXXXXX, USA 12345</li>
		<li class="phone">Phone: +01 888 (000) 1234</li>
		<li class="fax">Fax: +01 888 (000) 1234</li>
		<li class="email">Email: admin@v5cart.com</li>
		</ul>',
	'foot_note' => '
		<div class="row-fluid">	
		<div class="span9"><div class="box pav-custom">
		<div class="box-content"><div class="listmenu navbar">
		<ul class="nav">
		<li><a href="#">Links</a></li>
		</ul>
		</div>
		</div>
		</div>
		</div>
	    <div class="span3"><div class="box pav-custom  ">
		<div class="box-content"><div class="social">
		<h4 class="pull-left">Follow us on</h4>
			<div class="custom_follow">
			<a href="#" class="facebook">facebook</a>
			<a href="#" class="twitter">twitter</a>
			<a href="#" class="mail">Mails</a>
			<a href="#" class="rss">rss</a></div>
		</div>
		</div>
	 </div>
	 </div>
	 </div>',
);